<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            list-style: none;
        }

        .xlcaidan {
            width: 250px;
        }

        .xlcaidan p {
            height: 34px;
            line-height: 34px;
            padding-left: 20px;
            font-size: 13px;
            font-weight: 700;
            background-color: rgb(255, 250, 232);
        }

        .xlcaidan ul li {
            overflow: hidden;
            height: 0;
            transition: all .7s ease 0s;
            line-height: 34px;
            background-color: blue;
            font-size: 12px;
            padding-left: 30px;
            background-color: burlywood;
        }

        .gd {
            height: 34px !important;
        }
    </style>
</head>

<body>
    <div class="xlcaidan">
        <p>菜单1</p>
        <ul class="caidan0">
            <li>1</li>
            <li>2</li>
            <li>3</li>
        </ul>
        <p>菜单2</p>
        <ul class="caidan1">
            <li>1</li>
            <li>2</li>
            <li>3</li>
        </ul>
        <p>菜单3</p>
        <ul class="caidan2">
            <li>1</li>
            <li>2</li>
            <li>3</li>
        </ul>

    </div>
    <script>
        // 方法1
        var ps = document.querySelectorAll('p');
        var qlis = document.querySelectorAll('li');
        var num = 0;
        for (let i = 0; i < ps.length; i++) {
            ps[i].onclick = function () {
                var lis = document.querySelector(`.caidan${i}`).querySelectorAll('li');
                for (let k = 0; k < qlis.length; k++) {
                    qlis[k].style.height = '0px';
                }
                num++;
                if (num % 2 == 0) {
                    for (let j = 0; j < lis.length; j++) {
                        lis[j].style.height = '0px';
                    }

                } else {
                    for (let j = 0; j < lis.length; j++) {
                        lis[j].style.height = '34px';
                    }
                }
            }
        }
    </script>
</body>

</html>